<?php

	include_once("../../api/classes/gbif.occurrence.php");
	include_once("../../api/classes/gbif.eml.php");
	include_once("../../api/classes/zip/archive.php");
	
	$testFiles = array("test1.xls", "test2.xls");
	
	foreach($testFiles as $file) {
		print "Test File: " . $file . "<br>";
		$spreadsheet = new Occurrence( $file );
	#	print "<pre>";
		if ( $spreadsheet->validateMetaData() && $spreadsheet->validateOccurrenceData() ) {
			print "Passed";

			$folder = md5(mktime());
			mkdir("output/" . $folder);
			
			# Make EML
			$eml = new Eml();
			$worksheet = $spreadsheet->getSheet(1);
			$eml->createEmlFromWorksheet( $worksheet );
			$eml->generateXml(0);
			file_put_contents("output/" . $folder . "/eml.xml", $eml->getRawXml() );
	
			# Make Meta
			file_put_contents("output/" . $folder . "/meta.xml", $spreadsheet->generateOccurrenceDataMeta() );
			
			$spreadsheet->writeOccurenceData("output/" . $folder . "/occurrence.csv");
			$spreadsheet->zipArchive(
					"output/" . $folder . "/" . $folder . ".zip"
				, "output/" . $folder . "/"
				, array("eml.xml", "meta.xml", "occurrence.csv")
			);
	
		} else {
			print "Failed<hr>";
			$errors = $spreadsheet->getErrors();
			if (is_array($errors)) {
			foreach( $errors as $error ) {
				printf("%s (%s) - %s<br>", $error["type"], $error["code"], $error["value"]);
			}}
		}
		print "<br/><br/>";
	}
?>